package com.zpark.config;

import com.zpark.interceptor.MyInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.config.annotation.*;
import org.springframework.web.servlet.handler.SimpleMappingExceptionResolver;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

/**
 * SpringMVC配置类
 */
@Configuration
@ComponentScan(basePackages = "com.zpark") // 开启注解扫描的包
@EnableWebMvc  // 开启SpringMVC注解支持
public class SpringMVCConfig implements WebMvcConfigurer {

    @Autowired
    private MyInterceptor interceptor;

    /**
     * 配置拦截器   相当于： <mvc:interceptors>
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 创建自定义拦截器类对象
        // 添加拦截器
        InterceptorRegistration ireg = registry.addInterceptor(this.interceptor);
        // 添加拦截路径   <mvc:mapping path="/**"/>
        List<String> urls = new ArrayList<>();
        urls.add("/user/**");
        urls.add("/admin/**");
        ireg.addPathPatterns(urls);


        // 添加不拦截路径
        List<String> excludeUrls = new ArrayList<>();
        excludeUrls.add("/user/login.do");
        excludeUrls.add("/user/reg.do");
        excludeUrls.add("/admin/login.do");
        excludeUrls.add("/admin/login.do");
        excludeUrls.add("/css/**");
        excludeUrls.add("/js/**");
        excludeUrls.add("/html/**");
        ireg.excludePathPatterns(excludeUrls);
    }

    /**
     * 配置文件上传解析器
     * @return
     */
    @Bean
    public CommonsMultipartResolver multipartResolver(){
        CommonsMultipartResolver file = new CommonsMultipartResolver();
        file.setDefaultEncoding("UTF-8");
        file.setMaxUploadSize(1024 * 1024 * 10);

        return file;
    }

    /**
     * 作用放访问每某一个路径时跳转的页面
     * @param registry
     */
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/reg_page.do").setViewName("reg");
        registry.addViewController("/login_page.do").setViewName("login");
    }

    // 配置异常映射
    @Override
    public void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> resolvers) {
        SimpleMappingExceptionResolver resolver = new SimpleMappingExceptionResolver();

        Properties properties = new Properties();
        // 添加异常
        properties.setProperty("java.lang.Exception", "异常");
        properties.setProperty("com.zpark.exception.UserNotFoundException", "用户不存在异常");

        // 设置异常映射
        resolver.setExceptionMappings(properties);
        //设置共享异常信息的键
        resolver.setExceptionAttribute("ex");

        resolvers.add(resolver);
    }

    /**
     * 配置视图解析器
     */
    @Bean
    public InternalResourceViewResolver viewResolver(){
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();

        // 配置前缀
        resolver.setPrefix("/WEB-INF/pages/");
        // 配置后缀
        resolver.setSuffix(".jsp");

        return resolver;
    }

    /**
     * 格式化器
     */
    @Override
    public void addFormatters(FormatterRegistry registry) {
//        registry.addFormatter(null);
    }
}
